package com.sl.au.repository;

import com.sl.au.entity.Account;
import com.sl.au.entity.PayRegist;
import com.sl.au.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 回款历史
 * Created by RX on 2016-11-29.
 */
@Transactional(readOnly = true)
public interface PayRegistRepository extends JpaRepository<PayRegist, String> {
    /**
     * 获取回款历史数据
     * @param contractId
     * @param contractType
     * @return
     */
    @Query("select p from PayRegist p where p.contractId = ?1 and p.contractType=?2 and p.user.id=?3")
    Page<PayRegist> findByContractIdAndContractType(String contractId, String contractType,String userId,Pageable pageble);
    /**
     * 获取回款历史数据
     * @param contractId
     * @param contractType
     * @return
     */
    @Query("select p from PayRegist p where p.contractId = ?1 and p.contractType=?2")
    Page<PayRegist> findByContractIdAndContractType(String contractId, String contractType,Pageable pageble);
    /**
     * 得到回款历史数据条数
     * @param contractId
     * @return
     */
    @Query(" select count(*) as num from PayRegist p where p.contractId = ?1 and p.user.id=?2")
    Long getPayRegistCount(String contractId,String userId);
    /**
     * 得到回款历史数据条数
     * @param contractId
     * @return
     */
    @Query(" select count(*) as num from PayRegist p where p.contractId = ?1")
    Long getPayRegistCount(String contractId);

    /**
     * 得到修改回款之后的所有回款数据
     * @param contractId
     * @param createTime
     * @return
     */
    @Query("select p from PayRegist p where p.contractId = ?1 and p.createTime>=?2 order by p.createTime asc ")
    List<PayRegist> getChanges(String contractId, Date createTime);

    /**
     * 得到回款金额
     * @param contractId
     * @return
     */
    @Query("select p.paidSum from PayRegist p where p.contractId = ?1")
    float[] getPaySum(String contractId);

    /**
     * 得到所有回款历史
     * @param contractId
     * @param contractType
     * @return
     */
    @Query("select p from PayRegist p where p.contractId = ?1 and p.contractType=?2 order by p.createTime asc")
    List<PayRegist> findByContractIdAndContractType(String contractId, String contractType);
}
